package com.onefinger.manager.views.model
{
	import com.onefinger.manager.events.ManagerEvent;
	import com.onefinger.manager.vos.Category;
	import com.onefinger.manager.vos.Subcategory;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	public class SubCategoryCRUDModel extends BaseCRUDModel
	{
		//--------------------------------------------------------------------
		//    Public Setters and Getters
		//--------------------------------------------------------------------
		
		//.................categories......................
		private var _subcategories:ArrayCollection;
		
		[Bindable(event="changeSubCategories")]	
		public function get subcategories():ArrayCollection
		{
			return _subcategories;
		}
		public function set subcategories(value:ArrayCollection):void
		{
			_subcategories = value;
			dispatchEvent( new Event( "changeSubCategories" ) );
		}
		
		
		//.................categories......................
		private var _categories:ArrayCollection;
		
		[Bindable(event="changeCategories")]	
		public function get categories():ArrayCollection
		{
			return _categories;
		}
		public function set categories(value:ArrayCollection):void
		{
			_categories = value;
			dispatchEvent( new Event( "changeCategories" ) );
		}
		
		//.................selectedSubcategory......................
		private var _selectedSubcategory:Subcategory;
		
		[Bindable(event="changeSubcategory")]
		public function get selectedSubcategory():Subcategory
		{
			return _selectedSubcategory;
		}
		
		public function set selectedSubcategory(value:Subcategory):void
		{
			_selectedSubcategory = value;
			dispatchEvent( new Event( "changeSubcategory" ) );
		}
		
		//.................tempSubcategory......................
		private var _tempSubcategory:Subcategory=new Subcategory();
		
		[Bindable(event="changeTempSubcategory")]		
		public function get tempSubcategory():Subcategory
		{
			return _tempSubcategory;
		}
		
		public function set tempSubcategory(value:Subcategory):void
		{
			_tempSubcategory = value;
			dispatchEvent(new Event("changeTempSubcategory"));
		}
		
		//--------------------------------------------------------------------
		//    Constructor
		//--------------------------------------------------------------------
		public function SubCategoryCRUDModel(dispatcher:IEventDispatcher)
		{
			super(dispatcher);
		}
		//--------------------------------------------------------------------
		//    Public Methods
		//--------------------------------------------------------------------
		
		//.................selectSubcategory......................
		override public function select(subcategory:Object):void
		{
			super.select(subcategory);
			tempSubcategory = subcategory.clone();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_SUBCATEGORY );
			event.subcategory = subcategory as Subcategory;
			dispatcher.dispatchEvent( event );
		}
		override public function edit(subcategory:Object):void
		{
			super.edit(subcategory);
			select(subcategory);
			stateIndex = BaseCRUDModel.EDITING;
		}
		//.................deleteSubcategory......................
		override public function remove():void
		{
			super.remove();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.DELETE_SUBCATEGORY );
			event.subcategory = selectedSubcategory;
			dispatcher.dispatchEvent( event );
			unselect();
		}
		
		//.................addNewSubcategory......................
		override public function addNew():void
		{
			super.addNew();
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.SAVE_SUBCATEGORY);
			evet.subcategory = tempSubcategory;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateSubcategory......................
		override public function update():void
		{
			super.update();
			selectedSubcategory.name=tempSubcategory.name;
			selectedSubcategory.icon=tempSubcategory.icon;
			selectedSubcategory.category=tempSubcategory.category;
			selectedSubcategory.shortDescription = tempSubcategory.shortDescription;
			selectedSubcategory.showOrder = tempSubcategory.showOrder;
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.UPDATE_SUBCATEGORY);
			evet.subcategory = selectedSubcategory;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateSubcategory......................
		override public function unselect():void
		{
			super.unselect();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_SUBCATEGORY );
			event.subcategory = new Subcategory();
			dispatcher.dispatchEvent( event );
			tempSubcategory = new Subcategory();			
		}
		//.................cancel......................
		override public function cancel():void
		{
			super.cancel();
			selectedSubcategory= new Subcategory();
			tempSubcategory = new Subcategory();
			stateIndex=BaseCRUDModel.CREATING;
			
		}
		
		//--------------------------------------------------------------------
		//   update on change
		//--------------------------------------------------------------------
		
		public function updateSelectedIcon(value:String):void
		{
			tempSubcategory.icon=value;
		}
		public function updateSubcategoryName(value:String):void
		{
			tempSubcategory.name=value;	
		}
		public function updateShortDescription(value:String):void{
			tempSubcategory.shortDescription=value;
		}
		public function updateCategory(value:Category):void{
			tempSubcategory.category=value;
		}
		public function updateShowOrder(value:int):void{
			tempSubcategory.showOrder=value;
		}
		
	}
}